package com.smile.convert;

import com.smile.model.response.response.GetOrganizationListResponseDTO;
import com.smile.model.response.GetOrganizationListResponse;

import java.util.ArrayList;
import java.util.List;

public class OrganizationConverter {

    /**
     * 将DTO对象转换为Response对象（树形结构）
     */
    public static GetOrganizationListResponse convertToResponse(GetOrganizationListResponseDTO dto) {
        if (dto == null) {
            return null;
        }

        GetOrganizationListResponse response = new GetOrganizationListResponse();

        // 转换基础字段
        response.setId(dto.getId());
        response.setOrganizationName(dto.getOrganizationName());
        response.setChargeName(dto.getChargeName());
        response.setSort(dto.getSort());
        response.setCreateTime(dto.getCreateTime());
        response.setStatus(dto.getStatus());
        response.setOrganizationCode(dto.getOrganizationCode());
        response.setParentId(dto.getParentId());
        response.setLevel(dto.getLevel());
        response.setDeviceGroups(dto.getDeviceGroups());
        // 传递禁用状态
        response.setDisabled(dto.getDisabled());
        // 传递 ancestors 字段
        response.setAncestors(dto.getAncestors());

        // 递归转换子节点
        if (dto.getChildren() != null && !dto.getChildren().isEmpty()) {
            List<GetOrganizationListResponse> childResponses = new ArrayList<>();
            for (GetOrganizationListResponseDTO childDto : dto.getChildren()) {
                childResponses.add(convertToResponse(childDto));
            }
            response.setChildren(childResponses);
        }

        return response;
    }

    /**
     * 将DTO列表转换为Response列表（包含树形结构）
     */
    public static List<GetOrganizationListResponse> convertListToResponse(List<GetOrganizationListResponseDTO> dtoList) {
        List<GetOrganizationListResponse> responseList = new ArrayList<>();
        for (GetOrganizationListResponseDTO dto : dtoList) {
            responseList.add(convertToResponse(dto));
        }
        return responseList;
    }
}